﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Player : MovingObject {

    public int wallDamage = 1;
    public int pointsPerFood = 10;
    public int pointsPerSoda = 20;

    public float restartLevelDelay = 1f;

    private Animator animator;
    private int food;

    public Text foodText;

    private Vector2 touchOrigin = -Vector2.one;

	// Use this for initialization
	protected override void Start () {
        animator = GetComponent<Animator>();

        food = GameManager.instnce.playerFoodPoints;

        foodText.text = "Food:" + food;
        base.Start();
	}

    private void OnDisable()
    {
        GameManager.instnce.playerFoodPoints = food;
    }

    private void CheckIfGameOver()
    {
        print(food + " " + Time.deltaTime);
        print(food <= 0);
        if (food <= 0)
            GameManager.instnce.GameOver(); 
    }

    protected override void AtemptMove<T>(int xDir, int yDir)
    {
        food--;
        foodText.text = "Food:" + food;
        base.AtemptMove<T>(xDir, yDir);

        RaycastHit2D hit;

        CheckIfGameOver();

        GameManager.instnce.playersTurn = false;
    }
	
	// Update is called once per frame
	void Update () {
        if (!GameManager.instnce.playersTurn) return;

        int horizontal = 0;
        int vertical = 0;

#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBPLAYER

        horizontal = (int)Input.GetAxisRaw("Horizontal");
        vertical = (int)Input.GetAxisRaw("Vertical");

        if (horizontal != 0)
            vertical = 0;
#else
        if(Input.touchCount >0 )
        {
            Touch myTouch = Input.touches[0];

            if(myTouch.phase == TouchPhase.Began)
            {
                touchOrigin = myTouch.position;
            }else if(myTouch.phase == TouchPhase.Ended && touchOrigin.x >= 0){
                Vector2 touchEnd = myTouch.position;
                float x = touchEnd.x - touchOrigin.x;
                float y = touchEnd.y - touchOrigin.y;
                touchOrigin.x = -1;
                if(Mathf.Abs(x) > Mathf.Abs(y)){
                    horizontal = x > 0 ? 1: -1;
                }else{
                    vertical = y > 0 ? 1: -1;
                }
            }
        }
#endif

        if (horizontal != 0 || vertical != 0)
            AtemptMove<Wall>(horizontal, vertical);


	}

    protected override void OnCantMove<T>(T component)
    {
        Wall hitWall = component as Wall;
        hitWall.DanageWall(wallDamage);

        animator.SetTrigger("playerChop");
    }

    private void Restart()
    {
        Application.LoadLevel(Application.loadedLevel);
    }

    public void LoseFood(int loss)
    {
        animator.SetTrigger("PlayerHit");
        food -= loss;
        foodText.text = "-" + loss + " Food:" + food;
        CheckIfGameOver();
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Exit")
        {
            Invoke("Restart", restartLevelDelay);
            enabled = false;
        }else if (other.tag == "Food"){
            food += pointsPerFood;
            foodText.text = "+" + pointsPerFood + " Food:" + food;
            other.gameObject.SetActive(false);
        }else if (other.tag == "Soda"){
            food += pointsPerSoda;
            foodText.text = "+" + pointsPerSoda + " Food:" + food;
            other.gameObject.SetActive(false);
        }

    }
}
